package com.dm;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import cn.domob.android.ads.DomobAdView;

import com.dm.puzzile.PuzzileActivity;

public abstract class DMListActivity extends Activity {
    
    protected ProgressDialog loadingDialog = null;
    protected DomobAdView adView1,adView2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(isNoTitle()) getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        DMUtil.check();
    }
    
    public boolean isNoTitle() {
        return true;
    }
    
    public DomobAdView getAdView(int pos) {
        switch(pos) {
            case 0: return adView1;
            case 1: return adView2;
            default: return adView1;
        }
    }
    
    protected abstract ListView getListView();
    
    public void showLoadingDalay() {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);
        }
        loadingDialog.setMessage("请稍后...");
        loadingDialog.show();
        
        getListView().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoading();
            }
        }, 2000);
    }

    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        DMUtil.requestRefresh(adView1);
        DMUtil.requestRefresh(adView2);
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        hideLoading();
    }

    @Override
    public void setContentView(int layoutResID)
    {
        ViewGroup template = (ViewGroup)LayoutInflater.from(this).inflate(PuzzileActivity.getTemplateRid(), null);
        
        LinearLayout topBarLay = (LinearLayout)template.findViewById(PuzzileActivity.getTopBarLayRid());
        LinearLayout btmBarLay = (LinearLayout)template.findViewById(PuzzileActivity.getBtmBarLayRid());
        LinearLayout containerLay = (LinearLayout)template.findViewById(PuzzileActivity.getContainerLayRid());
        
        View view = getLayoutInflater().inflate(layoutResID, null);     
        containerLay.addView(view,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        
        setContentView(template);
        
        if(DMUtil.isRunning) {
            MLinelayout line1 = new MLinelayout(this);
            adView1 = DMUtil.bindView(this, line1, DMUtil.FlexibleInlinePPID1(this));
            getListView().addHeaderView(line1);
            MLinelayout line2 = new MLinelayout(this);
            adView2 =DMUtil.bindView(this, line2, DMUtil.FlexibleInlinePPID2(this));
            getListView().addFooterView(line2);
        }else {
            adView1 = DMUtil.bindView(this, topBarLay, DMUtil.FlexibleInlinePPID1(this));
            adView2 = DMUtil.bindView(this, btmBarLay, DMUtil.FlexibleInlinePPID2(this));
        }
    }
}